<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\Musics;
use App\Http\Model\Room;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\User;
use App\Lib\Search;


class MusicServices extends CommonServices
{

    public $musicModel;
    public $userModel;
    public $roomHostModel;
    public $roomModel;

    public function __construct()
    {
        $this->musicModel = new Musics();
        $this->userModel = new User();
        $this->roomHostModel = new RoomHostMicro();
        $this->roomModel = new Room();
        parent::__construct();
    }

    /**音乐列表
     * @param int $page
     * @param int $pageSize
     * @param string $musicName
     * @return array
     */
    public function getMusics(int $page, int $pageSize, string $musicName)
    {
        if ($musicName) {
            $musics = (new Search())->searchMusic($musicName, $page, $pageSize);
        } else {
            $musics = $this->musicModel->getList($page, $pageSize);
        }

        if (!empty($musics)) {
            $userIds = array_column($musics, 'user_id');
            $usersInfo = $this->userModel->get_users_by_ids($userIds);
            $usersInfo = array_column($usersInfo, null, 'user_id');

            foreach ($musics as $key => &$music) {
                $music['uploader'] = $usersInfo[$music['user_id']]['nickname']??'';
            }
        }

        return array('musics' => $musics);
    }

    /**上传音乐
     * @param array $data
     * @return int
     */
    public function uploadMusic(array $data = [])
    {
        $data['md5'] = md5($data['music_url']);
        $data['status'] = 0;
        $musicId = $this->musicModel->createData($data);
        if ($musicId) {
            return $musicId;
        }
        return Hint::ERR_FAIL;
    }

    /**删除我的音乐
     * @param int $musicId
     * @return int
     */
    public function deleteMusic(int $musicId)
    {
        $res = $this->musicModel->deleteMusic($musicId);
        if ($res) {
            $searchObj = new Search();
            $searchObj->delMusic($musicId);
            return Hint::SUCCESS;
        }
        return Hint::ERR_FAIL;
    }

    /**审核
     * @param int $musicId
     * @param int $status
     * @param int $checkId
     * @param string $remark
     * @return int
     */
    public function audit(int $musicId, int $status ,int $checkId ,string $remark)
    {
        $res = $this->musicModel->setAudit($musicId ,$status ,$checkId ,$remark);
        if($res){
            $searchObj = new Search();
            if($status == 1){
                $music = $this->musicModel->getMusicById($musicId);
                $searchObj->indexProMusic($music);
            }else {
                $searchObj->delMusic($musicId);
            }
            return  Hint::SUCCESS;
        }
        return  Hint::ERR_FAIL;
    }

    public function setPermission()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('room_id', '房间id', 'int', true, '房间id');
            $this->I('set_user_id', '设置权限的用户id', 'int', true, '设置权限的用户id');
            $this->I('user_id', '登录人id', true, '登录人id');
            $this->I('type', '类型', true, "1设置权限2取消权限");

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化
            $roleType = $this->roomModel->get_role($this->requestData['room_id'], $this->requestData['user_id']);
            if($roleType == 0){
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
            $type = $this->requestData['type'] ?? 1;

            if($type ==1){
                $this->musicModel->setMusicPermission($this->requestData['room_id'], $this->requestData['set_user_id']);
            }else{
                $this->musicModel->delMusicPermission($this->requestData['room_id'] ,$this->requestData['set_user_id']);
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    public function delPermission($room_id ,$user_id){
        $this->musicModel->delMusicPermission($room_id ,$user_id);
    }

    public function getPermission($room_id ,$user_id){
        $host_user_id = $this->roomHostModel->get_user_id($room_id);
        if($host_user_id == $user_id){
            return  1;
        }
        //add by zxiaofee 判断是不是管理员
        $role = $this->roomModel->get_role($room_id ,$user_id);
        if($role > 0){
            return 1;
        }
        return $this->musicModel->getMusicPermission($room_id ,$user_id);
    }
}